package com.firefly.service.query.mc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.mc.po.McMenuPo;
import com.firefly.dao.dao.mc.service.McMenuService;
import com.firefly.dao.assembler.mc.McMenuAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.vo.mc.McMenuTreeVo;
import com.firefly.dao.vo.mc.McMenuVo;
import com.firefly.util.utils.TreeNodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/1/31
 **/
@Service
public class McMenuQuery {
    private final McMenuService menuService;
    private final McMenuAssembler menuAssembler;
    @Autowired
    public McMenuQuery(McMenuService menuService, McMenuAssembler menuAssembler) {
        this.menuService = menuService;
        this.menuAssembler = menuAssembler;
    }
    // 所有数据的树形菜单
    public List<Map> listTree() {
        // 查出所有菜单来
        QueryWrapper<McMenuPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        List<McMenuPo> list = menuService.list(queryWrapper);
        // 类型转换
        List<McMenuTreeVo> listTree = menuAssembler.getTreeList(list);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toTree(0, listTree.stream().collect(Collectors.toList()));
        return result;
    }

    // 下拉菜单树
    public List<Map> selectTree() {
        QueryWrapper<McMenuPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id").select("id", "parent_id", "name");
        List<McMenuPo> list = menuService.list(queryWrapper);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toSelect(0, list.stream().collect(Collectors.toList()));
        return result;
    }

    // 单条信息
    public McMenuVo detail(CommonDto request) {
        McMenuPo menu = menuService.getById(request.getDetailId());
        McMenuVo result = menuAssembler.getVo(menu);
        return result;
    }
}
